import processing.core.PImage;

public class ButtonPlusMinusArtist extends Component {
	boolean pluState;
	public boolean pressed;
	public Artist artist;
	public boolean active;
	public CloseButton cb;


	////////////////////////////////////////////////////////////////////////////////////////////////////
	public ButtonPlusMinusArtist(String label, Artist artist)
	{
		super();
		pluState=true;
		width=10;
		height=10;
		this.label = label;
		this.artist = artist;
		active=false;
		cb = new CloseButton();
	}

	public void draw()
	{


		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
		GLOBAL.processing.textFont(GLOBAL.plotBoldFont, 14);
		GLOBAL.processing.stroke(128);
		GLOBAL.processing.strokeWeight(1);
		if(pluState)
			GLOBAL.processing.line(x+(int)((float)width/2 + 0.5), y, x+(int)((float)width/2 + 0.5), y+height);
		GLOBAL.processing.line(x, y+(int)((float)height/2 + 0.5), x+width, y+(int)((float)height/2 + 0.5));

		int cx = x;
		int cy = y;

		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);

		if(mouseOver()) 
		{
			super.draw();
		}


		GLOBAL.processing.strokeWeight(1);
		GLOBAL.processing.fill(GLOBAL.textArtistNameColor);
//		if(label == null)
//			System.out.println("ButtonPlusMinusArtist.draw()");
		if((int)(GLOBAL.processing.textWidth(label))>130){
			GLOBAL.processing.text(label.substring(0, GLOBAL.processing.min(16,label.length()))+"...", cx + 15, cy + 10);
		}
		else GLOBAL.processing.text(label, cx + 15, cy + 10);


		if(GLOBAL.processing.mouseX > x+width && 
				GLOBAL.processing.mouseX < x + 130 &&
				GLOBAL.processing.mouseY> y && GLOBAL.processing.mouseY < y +height
				&& (int)(GLOBAL.processing.textWidth(label))>130){
			drawMouseOver();

		}


		cb.active = true;
		cb.x=x + 200;
		cb.y=y;
		cb.draw();

	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	PImage icon;
	String label;


	// Draw entire label on mouseOver for too long labels
	public void drawMouseOver(){	
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		int w = (int)(GLOBAL.processing.textWidth(label));
		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
		GLOBAL.processing.noStroke();
		GLOBAL.processing.fill(GLOBAL.colorBackground);
		GLOBAL.processing.rect(x-130-20, y-10, w+10, 30);
		GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
		GLOBAL.processing.fill(GLOBAL.textColor);
		GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
		GLOBAL.processing.text(label, x-130-20, y-10 + 20);	
	}



	public void mouseCliked() {
		pluState =!pluState;
	}

	// Is mouse over Name of the Artist?
	public boolean mouseOverName()
	{
		if(GLOBAL.processing.mouseX > x + width && GLOBAL.processing.mouseX < x + width+20 + GLOBAL.processing.textWidth(label) && 
				GLOBAL.processing.mouseY > y && GLOBAL.processing.mouseY < y + height+5) 	return true;
		return false;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ButtonPlusMinusArtist other = (ButtonPlusMinusArtist) obj;
		if (artist == null) {
			if (other.artist != null)
				return false;
		} else if (!artist.equals(other.artist))
			return false;
		return true;
	}

	// Method called if the name of the artist is clicked
	public void nameClicked() {
		GLOBAL.addRemoveArtistToGraph(artist);
	}


}
